import time

import allure
from selenium.common import StaleElementReferenceException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from cloud_po.pages.base_page import BasePage
from cloud_po.utils.log_utils import logger


class ProductPage(BasePage):
    __ADD_PRODUCT_BTN = (By.XPATH, "//span[text()='添加']")
    __PRODUCT_NAME =  (By.CSS_SELECTOR,".px1>.col-12>span")
    __CLOSE_BTN = (By.XPATH,"//span[text()='提交']")


    __INPUT_SEARCH_NAME = (By.XPATH, "(//*[@class='el-input__inner'])[1]")
    __SEARCH_BTN = (By.XPATH, "(//*[@class='app-searchform bg-white p2']//div[@class='el-row']//button)[1]")
    __REMOVE_BTN = (By.XPATH, "//*[@class='app-searchform bg-white p2']//div[@class='el-row']//button[1]")
    __REMOVE_TAG = (By.XPATH, "//*[text()='已下架商品']")
    __REMOVE_PRODUCT_NAMES = (By.CSS_SELECTOR, ".px1>.col-12:nth-child(1)")
    __NORMAL_PRODUCT_TAG = (By.XPATH, "//*[text()='普通商品']")


    def click_add_product(self):
        logger.info("点击添加商品按钮")
        with allure.step("点击添加商品按钮"):
            self.do_find(self.__ADD_PRODUCT_BTN).click()
            time.sleep(1)
        from cloud_po.pages.product_add_page import ProductAddPage
        return ProductAddPage(self.driver)

    def get_product_names(self):
        # 这里应该查询所有数据，分页查，太复杂了，没做
        logger.info("获取所有商品名称")
        return self.get_names(self.__PRODUCT_NAME)

    def search_product(self,name):
        logger.info("根据商品名称搜索商品")
        with allure.step("商品名称输入框输入搜索内容"):
            self.do_send_keys(name, self.__INPUT_SEARCH_NAME)
        with allure.step("点击查询"):
            time.sleep(1)
            search_btn = self.do_find(self.__SEARCH_BTN)
            search_btn.click()
            time.sleep(1)
        return self.get_product_names()

    def restore_product(self):
        logger.info("还原商品列表状态")
        self.do_find(self.__INPUT_SEARCH_NAME).clear()


    def remove_product(self,name):
        logger.info("下架商品")
        rm_btn = self.do_find((By.XPATH,f"//*[text()='{name}']/../../../../../../div[2]//span[text()='下架']"))
        rm_btn.click()
        time.sleep(0.5)
        self.close_alert()


    def close_alert(self):
        logger.info("关闭弹窗")
        self.web_locate_click(self.__CLOSE_BTN)
        self.safe_click(self.__CLOSE_BTN)
        time.sleep(0.5)


    def click_remove_btn(self):
        logger.info("点击已下架商品标签页")
        self.do_find(self.__REMOVE_TAG).click()
        # self.web_locate_click(self.__REMOVE_TAG)
        # self.safe_click(self.__REMOVE_TAG)
        return ProductPage(self.driver)


    def put_on_product(self,name):
        logger.info("上架商品")
        put_element = (By.XPATH,f"//*[text()='{name}（已下架）']/../../../../../div[2]//*[text()='上架']")
        self.web_locate_click(put_element)
        self.safe_click(put_element)
        time.sleep(0.5)
        self.close_alert()
        return self.get_remove_product_names()

    def get_remove_product_names(self):
        logger.info("获取已下架商品页的商品名称列表")
        return self.get_names(self.__REMOVE_PRODUCT_NAMES)


    def click_normal_product(self):
        logger.info("点击普通商品标签")
        time.sleep(2)
        self.do_find(self.__NORMAL_PRODUCT_TAG).click()












